import {_EtInvoke, cancelRegWebNotify} from '@/views/office/et/js/et.js'
import {constEtEnum} from "@/components/WpsOffice/et/enum";
import {generateUUID} from "@/utils";
import * as FileApi from "@/api/infra/file";
const message = useMessage() // 消息弹窗

// 记录当前页面打开的wps
let activeEt = {};
// 异步的队列MAP
let etCallBackMap = {};

const getRouteName = function () {
  let hash = window.location.hash;
  if(hash.indexOf("#/redirect") != -1){
    hash = hash.replace("/redirect", "")
  }
  return hash;
}

const handleWpsMessage = function (clientType, name,route,activeEtId, data) {
  console.log(clientType, name,route,activeEtId, data,'ET-handleWpsMessage...clientType, clientType, name,route, data')

  let func = activeEt[data[constEtEnum.ActiveEtId]] ;

  if(data.msgType == "etClose"){
    delete activeEt[data[constEtEnum.ActiveEtId]] ;
    cancelRegWebNotify(data[constEtEnum.ActiveEtId]);
  }else if(data.msgType == "SaveET"){
    console.log('SaveET',data)
    let callEtTraceId = data[constEtEnum.CallEtTraceId]
    if(callEtTraceId != null ){
      let saveCallBackFuncMap = etCallBackMap[callEtTraceId]
      if(saveCallBackFuncMap != null  && typeof saveCallBackFuncMap.saveCallBackFunc == "function"){
        saveCallBackFuncMap.saveCallBackFunc(data,saveCallBackFuncMap.arguments)
      }
      delete etCallBackMap[callEtTraceId]
    }

  }
  if (typeof func == "function"){
    func(clientType, name,route,activeEtId, data)
  }
  else {
    console.log(data,'data')
  }
}

export const onUnmounted = function () {
  console.log('onUnmounted...')
  Object.keys(activeEt).forEach(function(key) {
    cancelRegWebNotify(activeEt[key]);
  });
}

export const newEt = function (callbackFunc) {
  console.log('newEt')
  let activeEtId = "wps-newEt-"+generateUUID();
  _EtInvoke([{
      "NewEt": {
        [constEtEnum.ActiveEtId]:activeEtId,
      }
    }],
    true,
    false,
    getRouteName(),
    activeEtId,
    handleWpsMessage
  ) // NewDoc方法对应于OA助手dispatcher支持的方法名
  activeEt[activeEtId] = callbackFunc;
  return activeEtId;
}



// 服务器文件地址
// https://code.996code.top:88/yudao-server/admin-api/infra/file/4/get/ba0af12f52535c646d66a1754796400db060f9f7cfa42f5e2d71ccc1ec298dc1.docx
export const openEt = function (openDocFileId,protectWorkbook,protectPwd,callbackFunc) {
  let activeEtId = "wps-openEt-"+generateUUID();

  FileApi.getFileById(openDocFileId)
    .then(fileInfo=>{
      console.log('文件信息',fileInfo);
      const  filePathUrl = fileInfo.url;
      const  fileName = fileInfo.name ;
      console.log('openEt',filePathUrl)
      let openEt =  {
        "filePathUrl": filePathUrl,
        "fileName": fileName,
        [constEtEnum.ActiveEtId]:activeEtId,
      };
      openEt["openType"] =  { //文档打开方式
        "protectWorkbook": protectWorkbook,
        "password": protectPwd
      }
      _EtInvoke([{
          "OpenEt":openEt
        }],
        true,
        false,
        getRouteName(),
        activeEtId,
        handleWpsMessage
      ) // NewDoc方法对应于OA助手dispatcher支持的方法名
      activeEt[activeEtId] = callbackFunc;
    })

  return activeEtId;
}
export const OnlineEditEt = async function (onlineEditDocFileId,callbackFunc) {
  let activeEtId = "wps-OnlineEditEt-"+generateUUID();

  await FileApi.getFileById(onlineEditDocFileId)
    .then(fileInfo=>{
      console.log('文件信息',fileInfo);
      const  filePathUrl = fileInfo.url;
      const  fileName = fileInfo.name ;
      console.log('OnlineEditEt',filePathUrl)
      let onlineEditDoc =  {
        "filePathUrl": filePathUrl,
        "fileName": fileName,
        [constEtEnum.ActiveEtId]:activeEtId,
      };

      _EtInvoke([{
          "OnlineEditEt": onlineEditDoc
        }],
        true,
        false,
        getRouteName(),
        activeEtId,
        handleWpsMessage
      ) // NewDoc方法对应于OA助手dispatcher支持的方法名
      activeEt[activeEtId] = callbackFunc;
    })
  return activeEtId;
}

export const SaveET = function (activeEtId, saveCallBackFunc) {
  console.log('SaveET')
  let callEtTraceId = generateUUID();
  _EtInvoke([{
      "SaveET": {
        [constEtEnum.CallEtTraceId]:callEtTraceId ,
        [constEtEnum.ActiveEtId]:activeEtId
      }
    }],
    false,
    false,
    getRouteName(),
    activeEtId)

  etCallBackMap[callEtTraceId] = {
    saveCallBackFunc:saveCallBackFunc,
    arguments:arguments
  }
}

export const SavePDF = function (activeEtId, saveCallBackFunc) {
  console.log('SavePDF')
  let callEtTraceId = generateUUID();
  _EtInvoke([{
      "SavePDF": {
        [constEtEnum.CallEtTraceId]:callEtTraceId ,
        [constEtEnum.ActiveEtId]:activeEtId
      }
    }],
    false,
    false,
    getRouteName(),
    activeEtId)
  etCallBackMap[callEtTraceId] = {
    saveCallBackFunc:saveCallBackFunc,
    arguments:arguments
  }
}


export const ExitET = function (activeEtId) {
  console.log('ExitET')
  _EtInvoke([{
    "ExitET": {
      [constEtEnum.ActiveEtId]:activeEtId
    }
  }], true,
    false,
    getRouteName(),
    activeEtId)
}


// 服务器文件地址
// https://code.996code.top:88/yudao-server/admin-api/infra/file/4/get/ba0af12f52535c646d66a1754796400db060f9f7cfa42f5e2d71ccc1ec298dc1.docx
export const PrintTemplateEt = function (openDocFileId,callbackFunc,dataInfo) {
  let activeEtId = "wps-printTemplateEt-"+generateUUID();

  FileApi.getFileById(openDocFileId)
    .then(fileInfo=>{
      console.log('文件信息',fileInfo);
      const  filePathUrl = fileInfo.url;
      // 打印模板，随机生成一个文件名
      const  fileName = generateUUID()+fileInfo.name ;
      console.log('PrintTemplateEt',filePathUrl)
      let PrintTemplateEt =  {
        "filePathUrl": filePathUrl,
        "fileName": fileName,
        [constEtEnum.ActiveEtId]:activeEtId,
        "dateInfo":dataInfo
      };
      _EtInvoke([{
          "PrintTemplateEt":PrintTemplateEt,
        }],
        false,
        true,
        getRouteName(),
        activeEtId,
        handleWpsMessage
      ) // NewDoc方法对应于OA助手dispatcher支持的方法名
      activeEt[activeEtId] = callbackFunc;
    })

  return activeEtId;
}
